﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace XCBPM.Mobile.BLL
{
    public class SysContext
    {
        private static string currentRoleID = "";

        public const string KEY_CACHESITE = "Cache:Site";

        public static string CurrentDomainName
        {
            get
            {
                return ConfigurationManager.AppSettings["DomainName"];
            }
        }


        public static string CurrentRoleID
        {
            get
            {
                return currentRoleID;
            }
            set
            {
                currentRoleID = value;
            }
        }



        public static string CurrentSiteNo
        {
            get
            {
                string number = "";
                try
                {
                    if (HttpContext.Current.Request["siteno"] != null)
                    {
                        return HttpContext.Current.Request["siteno"];
                    }
                    if ((IsValidCookies && (HttpContext.Current.Request.Cookies["Cache:Site"] != null)) && (HttpContext.Current.Request.Cookies["Cache:Site"].Value.Length > 0))
                    {
                        return HttpContext.Current.Request.Cookies["Cache:Site"].Value;
                    }
                    if (HttpContext.Current.Session["Cache:Site"] != null)
                    {
                        number = (string)HttpContext.Current.Session["Cache:Site"];
                    }
                    if (number.Length == 0)
                    {
                        //number = CurrentUser.OrganizationObject.Number;
                        if (IsValidCookies)
                        {
                            HttpContext.Current.Response.Cookies["Cache:Site"].Value = number;
                            HttpContext.Current.Request.Cookies["Cache:Site"].Value = number;
                            HttpContext.Current.Session["Cache:Site"] = number;
                        }
                    }
                }
                catch
                {
                }
                return number;
            }
            set
            {
                try
                {
                    if (IsValidCookies)
                    {
                        HttpContext.Current.Response.Cookies["Cache:Site"].Value = value;
                        HttpContext.Current.Request.Cookies["Cache:Site"].Value = value;
                    }
                }
                catch
                {
                }
                try
                {
                    if (string.IsNullOrEmpty(value))
                    {
                        HttpContext.Current.Session.Remove("Cache:Site");
                    }
                    else
                    {
                        HttpContext.Current.Session["Cache:Site"] = value;
                    }
                }
                catch
                {
                }
            }
        }
        private static bool IsValidCookies
        {
            get
            {
                return HttpContext.Current.Request.Browser.Cookies;
            }
        }

        public static string MachineName
        {
            get
            {
                string machineName = "";
                try
                {
                    machineName = HttpContext.Current.Server.MachineName;
                }
                catch
                {
                }
                return machineName;
            }
        }

        public static string SystemNo
        {
            get
            {
                return string.Format(ConfigurationManager.AppSettings["SystemNo"], new object[0]);
            }
        }

        public static string UserHostAddress
        {
            get
            {
                string userHostAddress = "";
                try
                {
                    userHostAddress = HttpContext.Current.Request.UserHostAddress;
                }
                catch
                {
                }
                return userHostAddress;
            }
        }

        public static string WorkFlowHost
        {
            get
            {
                return string.Format(ConfigurationManager.AppSettings["WorkflowHost"], new object[0]);
            }
        }
    }
}